//<noticesource filename="C:\Temp\noticesource\noticesource.xlsx" />
//<div id="mya" sourcetagname="noticesource" instancename="mynt"></div>
//var mynt = null;
//mynt = new NoticeT("mya");


var NoticeT = function(pid)
{
	this.rootid = pid;
	this.rootnode = document.getElementById(this.rootid);
	this.sourcetagname = this.rootnode.getAttribute("sourcetagname");
	this.instancename = this.rootnode.getAttribute("instancename");

	this.curtainsid = this.instancename + "curtain" + (new Date()).getTime().toString();
	this.divid = this.instancename + "div" + (new Date()).getTime().toString();

	this.dbfilename = "";
	this.initializeaccess = function()
	{
		var fu = new FileUtil();
		this.dbfilename = fu.gettempfilename(".accdb");
		(new AccessApp()).createdbfile(this.dbfilename);
		this.acc = new AccessClient(this.dbfilename);
	}
	this.initializeaccess();
	this.initializedesktop = function()
	{
		var myshell = new ActiveXObject("WScript.Shell");
		var mydesktoppath = myshell.SpecialFolders("Desktop");
		this.desktopfilename = (mydesktoppath + "\\NoticeList.xlsx");
		var fu = new FileUtil();
		if(!fu.fileexists(this.desktopfilename))
		{

			var excelapp = new ActiveXObject("Excel.Application");
			excelapp.DisplayAlerts = false;
			var book = excelapp.Workbooks.Add();
			var sheetmax = book.Worksheets.Count;
			for(var scnt = 0; scnt < sheetmax - 1; scnt++)
			{
				book.Worksheets.Item(sheetmax - scnt).Delete();
			}
			var sh = book.Worksheets.Item(1);
			sh.Name = "noticesource";
			var ir = sh.Range("A1");
			ir.Offset(0, 0).Value2 = "name";
			ir.Offset(0, 1).Value2 = "sourcetype";
			ir.Offset(0, 2).Value2 = "sourcefile";
			ir.Offset(0, 3).Value2 = "tablename";
			ir.Offset(0, 4).Value2 = "sql";
			ir.Offset(0, 5).Value2 = "charttype";
			ir.Offset(0, 6).Value2 = "charttitle";
			ir.Offset(0, 7).Value2 = "chartsql";
			ir.Offset(0, 8).Value2 = "chartaxis";
			ir.Offset(1, 0).Value2 = "desktopsource";
			ir.Offset(1, 1).Value2 = "excel";
			ir.Offset(1, 2).Value2 = this.desktopfilename;
			ir.Offset(1, 3).Value2 = "noticesource$A1:E100";
			ir.Offset(1, 4).Value2 = "select * from noticetable";
			ir.Offset(1, 5).Value2 = "xl3DColumn";
			ir.Offset(1, 6).Value2 = "sourcetypecount";
			ir.Offset(1, 7).Value2 = "select sourcetype, count(sourcetype) as SourceCount from noticetable group by sourcetype";
			ir.Offset(1, 8).Value2 = "sourcetype";
			var xlWorkbookDefault = 51;
			book.SaveAs(this.desktopfilename, xlWorkbookDefault);
			book.Close();
			excelapp.Quit();
		}
	}


	this.deflist = new Array();
	this.loadsource = function()
	{
		this.initializedesktop();
		this.deflist.length = 0;
		var app = new ActiveXObject("Excel.Application");
		app.DisplayAlerts = false;
		var nodelist = document.getElementsByTagName(this.sourcetagname);
		var filelist = new Array();
		filelist.length = 0;
		for(var icnt = 0; icnt < nodelist.length; icnt++)
		{
			filelist[filelist.length] = nodelist[icnt].getAttribute("filename");
		}
		filelist[filelist.length] = this.desktopfilename;
		for(var icnt = 0; icnt < filelist.length; icnt++)
		{
			var myfn = filelist[icnt];
			var book = app.Workbooks.Open(myfn, null, true);
			var sheet = book.Worksheets.Item("noticesource");
			var ir = sheet.Range("A1");
			var rowoffset = 0;
			while(true)
			{
				rowoffset++;
				var myrowcell = ir.Offset(rowoffset, 0);
				if(myrowcell.Text == "")
				{
					break;
				}
				else
				{
					var tempo = new Array();
					tempo.length = 0;
					tempo.name = myrowcell.Text;
					tempo.sourcetype = myrowcell.Offset(0, 1).Text;
					tempo.sourcefile = myrowcell.Offset(0, 2).Text;
					tempo.tablename = myrowcell.Offset(0, 3).Text;
					tempo.sqloriginal = myrowcell.Offset(0, 4).Text;
					tempo.charttype = myrowcell.Offset(0, 5).Text;
					tempo.charttitle = myrowcell.Offset(0, 6).Text;
					tempo.chartsql = myrowcell.Offset(0, 7).Text;
					tempo.chartaxis = myrowcell.Offset(0, 8).Text;
					tempo.sql = tempo.sqloriginal;
					switch(tempo.sourcetype)
					{
						case "access":
							{
								tempo.sql = (new StrUtil()).replaceall(tempo.sql, "noticetable", tempo.tablename + " in '" + tempo.sourcefile + "'");
								tempo.chartsql = (new StrUtil()).replaceall(tempo.chartsql, "noticetable", tempo.tablename + " in '" + tempo.sourcefile + "'");
								break;
							}
						case "excel":
							{
								tempo.sql = (new StrUtil()).replaceall(tempo.sql, "noticetable", "[Excel 8.0;database=" + tempo.sourcefile + "].[" + tempo.tablename + "]");
								tempo.chartsql = (new StrUtil()).replaceall(tempo.chartsql, "noticetable", "[Excel 8.0;database=" + tempo.sourcefile + "].[" + tempo.tablename + "]");
								break;
							}
						default:
							{
								break;
							}
					}

					this.deflist[this.deflist.length] = tempo;
				}
			}
			book.Close();
		}
		app.Quit();
	}

	this.generatedateid = function()
	{
		var ret = (new Date()).getTime().toString();
		ret += (new Date()).getTime().toString();
		return ret;
	}

	this.reloadtables = function()
	{
		this.generatetable();
	}

	this.reloadall = function()
	{
		this.loadsource();
		this.reloadtables();
	}

	this.generatetable = function()
	{
		var mya = document.getElementById(this.rootid);
		mya.innerHTML = "";
		var gt = mya.appendChild(document.createElement("table"));
		var gr = gt.appendChild(document.createElement("tr"));
		var gd = gr.appendChild(document.createElement("td"));
		var myi = gd.appendChild(document.createElement("input"));
		myi.setAttribute("type", "button");
		myi.style.background = "transparent";
		myi.style.border = "none";
		myi.style.color = "blue";
		myi.setAttribute("onclick", this.instancename + ".reloadtables();");
		myi.setAttribute("value", "reload tables");

		myi = gd.appendChild(document.createElement("input"));
		myi.setAttribute("type", "button");
		myi.style.background = "transparent";
		myi.style.border = "none";
		myi.style.color = "blue";
		myi.setAttribute("onclick", this.instancename + ".reloadall();");
		myi.setAttribute("value", "reload all");

		gr = gt.appendChild(document.createElement("tr"));
		gd = gr.appendChild(document.createElement("td"));
		var ret = gd.appendChild(document.createElement("table"));
		ret.style.fontSize = "small";

		for(var icnt = 0; icnt < this.deflist.length; icnt++)
		{
			var defe = this.deflist[icnt];
			var retr = null;
			var retd = null;
			retr = ret.appendChild(document.createElement("tr"));
			retr.style.backgroundColor = "#e6e6fa";
			retd = retr.appendChild(document.createElement("td"));
			retd.innerText = defe.name;

			var rid = icnt.toString() + "rowwww_" + this.generatedateid();
			var did = rid + "_dattttttt" + this.generatedateid();
			retd.setAttribute("onclick", "var mya = document.getElementById('" + rid + "'); if(mya.style.display == ''){mya.style.display = 'none';}else{mya.style.display = '';}");
			retd.setAttribute("oncontextmenu", this.instancename + ".displaychart('" + did + "');");

			retd = retr.appendChild(document.createElement("td"));
			retd.setAttribute("onclick", this.instancename + ".generateinnertable('" + did + "');");
			retd.style.color = "blue";
			retd.innerText = "reload";

			retd = retr.appendChild(document.createElement("td"));
			var myanc = retd.appendChild(document.createElement("a"));
			myanc.setAttribute("href", defe.sourcefile);
			myanc.setAttribute("target", this.generatedateid());
			myanc.innerText = defe.sourcefile;

			retr = ret.appendChild(document.createElement("tr"));
			retr.setAttribute("id", rid);
			retr.style.display = "none";
			retd = retr.appendChild(document.createElement("td"));
			retd.setAttribute("colspan", "3");

			retd.setAttribute("id", did);
			retd.setAttribute("sql", defe.sql);
			retd.setAttribute("charttype", defe.charttype);
			retd.setAttribute("charttitle", defe.charttitle);
			retd.setAttribute("chartsql", defe.chartsql);
			retd.setAttribute("chartaxis", defe.chartaxis);
			this.generateinnertable(did);
		}
	}

	this.generateinnertable = function(pid)
	{
		var mya = document.getElementById(pid);
		var tempt = document.createElement("table");
		var tempr = null;
		var tempd = null;
		try
		{
			var rl = this.acc.executeselectquery(mya.getAttribute("sql"));
			var cols = new Array();
			cols.length = 0;
			for(var rcnt = 0; rcnt < rl.length; rcnt++)
			{
				var re = rl[rcnt];
				if(rcnt == 0)
				{
					tempr = tempt.appendChild(document.createElement("tr"));
					tempr.style.backgroundColor = "#f0e68c";
					for(pn in re)
					{
						cols[cols.length] = pn;
						tempd = tempr.appendChild(document.createElement("td"));
						tempd.innerText = pn;
					}
				}
				tempr = tempt.appendChild(document.createElement("tr"));
				tempr.style.backgroundColor = "#ffffe0";
				for(var ccnt = 0; ccnt < cols.length; ccnt++)
				{
					var colnam = cols[ccnt];
					tempd = tempr.appendChild(document.createElement("td"));
					try
					{
						tempd.innerHTML = (new StrUtil()).replaceall(re[colnam], "\n", "<br />");
					}
					catch(eerrr)
					{
						tempd.innerText = re[colnam];
					}
				}
			}
		}
		catch(err)
		{
			tempr = tempt.appendChild(document.createElement("tr"));
			tempd = tempr.appendChild(document.createElement("td"));
			tempd.innerText = err.message;
		}
		mya.innerHTML = tempt.outerHTML;
	}

	this.displaychart = function(pid)
	{
		try
		{
			var mya = document.getElementById(pid);
			var ctitle = mya.getAttribute("charttitle");
			var ctype = mya.getAttribute("charttype");
			var csql = mya.getAttribute("chartsql");
			var caxis = mya.getAttribute("chartaxis");
			var rl = this.acc.executeselectquery(csql);
			var factory = new ChartGenerator();
			var fn = factory.createchart(ctitle, ctype, rl, caxis);
			this.displaycurtain();
			this.displayimage(fn);
		}
		catch(err)
		{
			this.closecurtain();
			this.displaycurtain(err.message);
		}
	}

	this.displaycurtain = function(pmessage)
	{
		var canv = document.body.appendChild(document.createElement("canvas"));
		canv.style.position = "absolute";
		canv.style.top = 0;
		canv.style.left = 0;
		canv.style.zIndex = 100;
		canv.width = window.innerWidth;
		canv.height = window.innerHeight;
		var cx = canv.getContext("2d");
		cx.beginPath();
		cx.fillStyle = "rgba(0,0,0,0.7)";
		cx.fillRect(0,0,canv.width, canv.height);
		if(pmessage)
		{
			cx.fillStyle = "white";
			cx.font = "20px 'MS UI Gothic'";
			var ml = pmessage.split("\n");
			for(var icnt = 0; icnt < ml.length; icnt++)
			{
				cx.fillText(ml[icnt], 100, 200 + 50 * icnt);
			}
		}
		cx.closePath();
		canv.setAttribute("id", this.curtainid);
		canv.setAttribute("onclick", this.instancename + ".closecurtain();");
	}
	this.displayimage = function(pfn)
	{
		var mydiv = document.body.appendChild(document.createElement("img"));
		mydiv.setAttribute("id", this.divid);
		mydiv.style.position = "absolute";
		mydiv.style.top = "100px";
		mydiv.style.left = "100px";
		mydiv.style.zIndex = 101;
		mydiv.width = window.innerWidth - 100 * 2;
		mydiv.height = window.innerHeight - 100 * 2;
		mydiv.setAttribute("src", pfn);
	}
	this.closecurtain = function()
	{
		var nl = new Array();
		nl.length = 0;
		nl[nl.length] = this.curtainid;
		nl[nl.length] = this.divid;

		for(var icnt = 0; icnt < nl.length; icnt++)
		{
			try
			{
				var mya = document.getElementById(nl[icnt]);
				mya.parentNode.removeChild(mya);
			}catch(err){}
		}
	}

	this.loadsource();
	this.generatetable();
}

